var postlist_apis = {
	post_list_by_module: "http://api.fangchepingtai.com/fangchebbs/post/listbymodule/",
	post_list_by_page: "http://api.fangchepingtai.com/fangchebbs/post/listbypage",
	post_module_info: "http://api.fangchepingtai.com/fangchebbs/module/info/",
	post_count: "http://api.fangchepingtai.com/fangchebbs/post/count",
};
(function() {
	var _module, page, num, pages;

	function getPostList(_module, page, num) {
		var get_url = _module ? postlist_apis.post_list_by_module + _module : postlist_apis.post_list_by_page;
		coAjax.get(get_url, {
			page: page,
			num: num
		}, function(result) {
			console.log(result);
			App.set("$Cache.postlist.list", result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
		_module ? coAjax.get(postlist_apis.post_module_info + _module, function(result) {
			App.set("$Cache.postlist.module", result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		}) : false;
	};

	// 获取对应话题统计
	function getCount(_module) {
		var data = _module ? {
			_module: _module
		} : {};
		coAjax.get(postlist_apis.post_count, data, function(result) {
			console.log(result);
			// 计算分页
			pages = [];
			pages.length = Math.ceil(result / num);
			App.set("$Cache.postlist.pages", pages);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	};

	// 翻页
	App.set("$Event.postlist.prePostPage", function() {
		var jumpPage = App.get("$Cache.postlist.current_page");
		jumpPage > 0 ? jumpPage-- : jumpPage = 0;
		Path.jump("/postlist.html?_module=" + _module + "&page=" + jumpPage + "&num=" + num);
	});
	App.set("$Event.postlist.nextPostPage", function() {
		var jumpPage = App.get("$Cache.mian.current_page");
		jumpPage < pages.length - 1 ? jumpPage++ : jumpPage = pages.length - 1;
		Path.jump("/postlist.html?_module=" + _module + "&page=" + jumpPage + "&num=" + num);
	});


	Path.on("/__basename__.html", function(_current_location) {
		var qs = _current_location.query;
		_module = qs.get("_module");
		page = qs.get("page") || 0;
		App.set("$Cache.postlist.current_page", page);
		num = qs.get("num") || 15;
		App.set("$Cache.postlist.page_num", num);
		getPostList(_module, page, num);
		getCount(_module);

	});

}());